﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace QT.SEO
{
    /// <summary>
    /// 搜索快照类
    /// </summary>
    public class SnapShot
    {
        /// <summary>
        /// 获取指定URL的所有快照记录
        /// </summary>
        /// <param name="guid">网址ID</param>
        /// <returns>DataTable</returns>
        public static DataTable Get(string guid)
        {
            string sql = @"select a.[id],a.[snapDate],a.[checkDate],b.[SearchName] from [QT_Data_SnapShot] a left join [QT_Basic_SearchEngine] b on a.[sid]=b.[sid] where a.[uid]='" + guid + "' order by a.[sid], CDate(a.[CheckDate]) asc";

            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);

            return dt;
        }

        /// <summary>
        /// 获取指定分组的网址、指定搜索引擎的收录量
        /// </summary>
        /// <param name="gids">一组分组ID</param>
        /// <param name="sid">搜索引擎ID</param>
        /// <returns>string</returns>
        public static string GetRecordCount(List<string> gids,string sid)
        {
            string sql = "select count(*) from [QT_Data_SnapShot] where [Sid]='" + sid + "' and [uid] in(select [guid] from [QT_Data_URLList] where [gid] in('" + string.Join(",", gids.ToArray()) + "'))";
            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
            return dt.Rows[0][0].ToString();
        }

        /// <summary>
        /// 添加记录,-1为失败；1为成功；0为已存在
        /// </summary>
        /// <param name="uid">网址</param>
        /// <param name="date">快照日期</param>
        /// <param name="sid">搜索引擎ID</param>
        /// <param name="title">网页标题</param>
        /// <param name="snapshot">快照地址</param>
        /// <returns>int</returns>
        public static int Add(string uid, string date, string sid, string title,string snapshot)
        {
            if (IsExsit(uid, date)) return -2;

            int rVal = 0;

            string sql = @"insert into [QT_Data_SnapShot]([id],[uid],[SnapDate],[CheckDate],[sid],[Title],[snapshot]) values('" + Guid.NewGuid().ToString().Replace("-", "") + "','" + uid + "','" + date + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + sid + "','" + title + "','" + snapshot + "')";
            rVal = new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);

            return rVal;
        }

        ///// <summary>
        ///// 查询快照记录是否已存在
        ///// </summary>
        ///// <param name="url">快照对应的URL</param>
        ///// <param name="date">快照日期</param>
        ///// <returns>bool</returns>
        //public static bool IsExsit(string url, string date)
        //{
        //    string sql = @"select [SnapDate] from [QT_Data_SnapShot] where [url]='" + url + "' and [SnapDate]='" + date + "'";

        //    DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);

        //    if (dt.Rows.Count > 0) return true;

        //    return false;
        //}
        
        /// <summary>
        /// 查询快照记录是否已存在
        /// </summary>
        /// <param name="uid">URLd的ID</param>
        /// <param name="date">快照日期</param>
        /// <returns>bool</returns>
        public static bool IsExsit(string uid, string date)
        {
            string sql = @"select [SnapDate] from [QT_Data_SnapShot] where [uid]='" + uid + "' and [SnapDate]='" + date + "'";

            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);

            if (dt.Rows.Count > 0) return true;

            return false;
        }
    }
}
